<template>
<div class="login">
  <div class="login_container">
    <div class="avatar">
      <img src="../assets/img/logo.png" alt="头像">
    </div>
    <div class="input_form">
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" placeholder="输入用户名" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" placeholder="输入密码" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="loginFormReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</div>
</template>

<script>
export default {
  name: 'login',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        /* 验证规则的名称必须和输入框绑定的名称相同 */
        username: [
          { required: true, message: '用户名称不能为空', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }

        ]
      }
    }
  },
  methods: {
    // 重置按钮，重置表单属性
    loginFormReset () {
      // console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    // 登录发送请求
    login () {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) {} else {
          const { data: result } = await this.$axios.post('login', this.loginForm)
          await console.log(result)
          if (result.meta.status === 200) {
            console.log('登陆成功')
            this.$message.success('你成功了sb')
            window.sessionStorage.setItem('token', result.data.token)
            await this.$router.push('/home')
          } else {
            console.log('登陆失败')
            this.$message.error('你失败了sb')
          }
        }
      })
    }
  }
}

</script>
<style scoped lang="less">
.login {
  background: #9275e3;
  height: 100%;
  .login_container{
    position: absolute;
    top:50%;
    right:50%;
    transform: translate(50%,-50%);
    height: 300px;
    width: 480px;
    border: black solid 1px;
    background: #eeeeee;
    .avatar {
      width: 130px;
      height: 130px;
      border: #eee solid 2px;
      box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2) ,0 6px 20px 0 rgba(0, 0, 0, 0.19);
      position: absolute;
      top:0;
      right:50%;
      transform: translate(50%,-50%);
      border-radius: 50% ;
      background: aliceblue;
      img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
        border: #eee solid 1px;
      }
    }
    .input_form {
      width: 100%;
      position: absolute;
      bottom: 0;
      padding: 0 20px;
      box-sizing: border-box;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }

  }
}

</style>
